#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/9/9 16:58
# @Author : lenovo
# @Email : 26541796406@qq.com
# @File : CRM_ST_BG_030.py
# @Project : crm
"""修改任务正确输入所有信息  断言时可判断页面是否出现任务修改成功"""
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
"""选择Chrome浏览器，将driverAlert化，打开crm网页"""
driver = webdriver.Chrome()
driver.get('http://192.168.1.139/crm3')
sleep(2)
"""找到用户名，密码输入框以及登录按钮"""
username_locator = driver.find_element_by_name('name')
password_locator = driver.find_element_by_name('password')
login_locator = driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/form/fieldset/input[3]")
"""输入用户名，密码，点击登录"""
username_locator.send_keys('admin')
password_locator.send_keys('123456')
login_locator.click()
sleep(3)
"""
 这是在获取cookies值
 以后就可以免登录，直接绕过登录页面
cookies = driver.get_cookies()
cookie = {}
for k,v in cookies[0].items():
    if k == 'name' or k == 'value':
        cookie[k] = v
print(cookie)
"""
cookie = {'name': 'PHPSESSID', 'value': 'i1mj8tbl7cp4pt11rijh3toul0'}
driver.get('http://192.168.1.139')
driver.add_cookie(cookie)
"""直接获得修改任务的网址"""
driver.get('http://192.168.1.139/crm3/index.php?m=task')
sleep(2)
"""进行修改任务的操作"""
# 添加定位器
amending_locator = (By.LINK_TEXT,'修改')
theme_locator = (By.NAME,'subject')  # 主题输入框
head_locator = (By.ID,"owner_name") # 负责人输入框
almost_locator = (By.XPATH,'/html/body/div[7]/div[2]/form/div/input')  # 全选input
choose_head_locator = (By.CSS_SELECTOR,'input[rel="wuyifan"]')  # 选择负责人
ok_locator = (By.XPATH,'/html/body/div[7]/div[3]/div/button[1]')  # OK按钮
director_locator = (By.ID,'about_roles_name')  # 相关负责人输入框
all_locator = (By.XPATH,'/html/body/div[8]/div[2]/form/div/input')  # 全选input
choose_director_locator = (By.CSS_SELECTOR,'input[rel="admin"]')  # 选择相关负责人
determine_locator = (By.XPATH,'/html/body/div[8]/div[3]/div/button[1]/span')  # 确定按钮
date_locator = (By.NAME,'due_date')  # 截至日期
describe_locator = (By.XPATH,'/html/body')  # 描述输入框
submit_locator = (By.CSS_SELECTOR,'input[value="保存"]')  # 保存按钮
# 具体操作
driver.find_elements(*amending_locator)[0].click()
driver.find_element(*theme_locator).clear()
driver.find_element(*theme_locator).send_keys('9月份去广州、沈阳出差')  # 在主题输入框输入主题名称
driver.find_element(*head_locator).click()  # 点击负责人输入框
sleep(3)
driver.find_element(*almost_locator).click()
sleep(2)
driver.find_element(*almost_locator).click()
sleep(1)
driver.find_element(*choose_head_locator).click()  # 选择负责人
sleep(1)
driver.find_element(*ok_locator).click()  # 点击确定
driver.find_element(*director_locator).click()  # 点击相关负责人输入框
sleep(2)
driver.find_element(*all_locator).click()
sleep(2)
driver.find_element(*all_locator).click()
driver.find_element(*choose_director_locator).click()  # 选择负责人
driver.find_element(*determine_locator).click()  # 点击确定
sleep(2)
driver.find_element(*date_locator).clear()
driver.find_element(*date_locator).send_keys('2021-11-04')  # 输入截止日期
sleep(2)
driver.switch_to.frame(driver.find_element_by_class_name('ke-edit-iframe'))
driver.find_element(*describe_locator).clear()
driver.find_element(*describe_locator).send_keys('别想住酒店')  # 输入描述
driver.switch_to.parent_frame()
driver.find_element(*submit_locator).click()  # 点击保存

sleep(3)
driver.close()